package com.hd.appcommon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.appcommon.entity.RoleDept;
import com.hd.appcommon.entity.vo.DeptSelectVo;
import com.hd.appcommon.mapper.RoleDeptMapper;
import com.hd.appcommon.service.RoleDeptService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author administrator
 * @since 2020-11-24
 */
@Service
public class RoleDeptServiceImpl extends ServiceImpl<RoleDeptMapper, RoleDept> implements RoleDeptService {

    @Override
    public void insertTeacherAndStuDept(String roleId, String[] teacherDept, String[] stuDept) {
        Date date = new Date();
        if (null != teacherDept) {
            List<String> teacherDeptList = Arrays.asList(teacherDept);

            teacherDeptList.parallelStream().forEach(
                    td -> {
                        RoleDept roleDept = new RoleDept();
                        roleDept.setDept(Long.valueOf(td));
                        roleDept.setRoleId(roleId);
                        roleDept.setDeptType(1);
                        roleDept.setCreateTime(date);
                        this.save(roleDept);
                    }
            );
        }
        if (null != stuDept) {
            List<String> stuDeptList = Arrays.asList(stuDept);
            stuDeptList.parallelStream().forEach(
                    sd -> {
                        RoleDept roleDept = new RoleDept();
                        roleDept.setDept(Long.valueOf(sd));
                        roleDept.setRoleId(roleId);
                        roleDept.setDeptType(2);
                        roleDept.setCreateTime(date);
                        this.save(roleDept);
                    }
            );
        }
    }

    @Override
    public List<DeptSelectVo> getDeptsVoByRoleId(String roleId , Integer type) {
        return this.baseMapper.getDeptsVoByRoleId(roleId,type);
    }

}
